package prettypenny;

import java.util.*;
import java.sql.*;

public class Lists {

    private ArrayList<Client> clients;
    private Connection con;
    private Statement stmt;
    private ResultSet rs;
    private int position;
    /* DB Struture
     * car: carId, make, model, fuelType, regNum
     * carService: serviceID, clientId, carId, locationId, mechanicId, typeId, serviceDate, serviceTime, description, price, bookingDate, mileage
     * client: clientId, forname, surname, dob, addLine1, addLine2, city, county, postcode, homeNum, mobileNum, email
     * location: locationId, branch
     * mechanic: mechanicId, forename, surname, mobNum
     * serviceType: typeId, typeOfService
     */

    public Lists() {
        try { // Load all the values on database at once.

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // server:port:database
            con = DriverManager.getConnection("jdbc:odbc:uni");
            stmt = con.createStatement();

        } catch (Exception e) {
            System.out.println(e.toString());
        }
        RefreshData();
    }
    
    public void RefreshData() {
            // Clients
        int count = 0;
        try {
            clients = new ArrayList<Client>();
            rs = stmt.executeQuery("SELECT * FROM client");
            while (rs.next()) { // TODO
                count++;
                System.out.println(count);
                clients.add(new Client(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12)));
            }
        }catch(Exception ex) {
            System.out.println("SS"+ex.toString());
        }
    }
   
    public ArrayList getClients() {
        return this.clients;
    }

    public Client getFirst() {
        position = 0;
        return clients.get(position);
    }

    public Client getLast() {
        position = clients.size() - 1;
        return clients.get(position);
    }

    public Client getNext() {
        if (position < clients.size()) {
            position++;
        }
        return clients.get(position);
    }
    public Client getPosition(int value) {
        return clients.get(value-1);
    }

    public Client getPrevious() {
        if (position > 0) {
            position--;
        }
        return clients.get(position);
    }

    public int getClientSize() {
        return clients.size() - 1;
    }
}
